import mysql.connector

source_conn = mysql.connector.connect(
    host='192.168.3.138',
    user='test123',
    password='test1234',
    database='gwzadb'
)
source_cursor = source_conn.cursor()

destination_conn = mysql.connector.connect(
    host='192.168.3.138',
    user='test123',
    password='test1234',
    database='gwzadb'
)
destination_cursor = destination_conn.cursor()

source_table = 'map_device_copy1'
destination_table = 'map_device'

anchor_list = ['GW4368'
,'GW5329'
,'GW4D34'
,'GW16B7'
,'GW161B'
,'GW19AF'
,'GW1D1E'
,'GW5885'
,'GW583B'
,'GW1781'
,'GW4ECE'
,'GW491A'
,'GW4112'
,'GW1969'
,'GW4E0C'
,'GW4D98'
,'GW448F'
,'GW4280'
,'GW44AB'
,'GW1D35'
,'GW15B1'
,'GW413A'
,'GW4EB9'
,'GW44B1'
,'GW5335'
,'GW15AB'
,'GW53E5'
,'GW15B6'
,'GW15EB'
,'GWD118'
,'GW4026'
,'GWD10A'
,'GW40C0'
,'GW4080']

for anchor in anchor_list:
    query = f"SELECT x, y, z FROM {source_table} WHERE device_id='{anchor}'"
    source_cursor.execute(query)
    result = source_cursor.fetchone()
    if result is not None:
        query = f"SELECT device_id FROM {destination_table} WHERE device_id='{anchor}'"
        destination_cursor.execute(query)
        if (destination_cursor.fetchone() is None):
            query = f"INSERT INTO {destination_table} (device_id, x, y, z) VALUES (%s, %s, %s,%s)"
        else:
            query = f"UPDATE {destination_table} SET x = %s, y = %s, z = %s WHERE device_id = %s"
        destination_cursor.execute(query, (result[0], result[1], result[2], anchor))
    else:
        print("查询结果为空")
destination_conn.commit()
destination_conn.close()
source_conn.close()
import mysql.connector

source_conn = mysql.connector.connect(
    host='192.168.3.44',
    user='test123',
    password='test1234',
    database='gwza172gateway'
)
source_cursor = source_conn.cursor()

destination_conn = mysql.connector.connect(
    host='192.168.3.138',
    user='test123',
    password='test1234',
    database='gwzadb'
)
destination_cursor = destination_conn.cursor()

source_table = 'map_device'
destination_table = 'map_device'

anchor_list = ['GW4368'
,'GW5329'
,'GW4D34'
,'GW16B7'
,'GW161B'
,'GW19AF'
,'GW1D1E'
,'GW5885'
,'GW583B'
,'GW1781'
,'GW4ECE'
,'GW491A'
,'GW4112'
,'GW1969'
,'GW4E0C'
,'GW1839'
,'GW448F'
,'GW4280'
,'GW44AB'
,'GW1D35'
,'GW15B1'
,'GW413A'
,'GW4EB9'
,'GW44B1'
,'GW5335'
,'GW15AB'
,'GW53E5'
,'GW15B6'
,'GW15EB'
,'GWD118'
,'GW4026'
,'GWD10A'
,'GW40C0'
,'GW4080']

for anchor in anchor_list:
    query = f"SELECT x, y, z FROM {source_table} WHERE device_id='{anchor}'"
    source_cursor.execute(query)
    result = source_cursor.fetchone()
    if result is not None:
        query = f"SELECT device_id FROM {destination_table} WHERE device_id='{anchor}'"
        destination_cursor.execute(query)
        if (destination_cursor.fetchone() is None):
            query = f"INSERT INTO {destination_table} (device_id, x, y, z) VALUES (%s, %s, %s,%s)"
            destination_cursor.execute(query, (anchor,result[0], result[1], result[2]))
        else:
            query = f"UPDATE {destination_table} SET x = %s, y = %s, z = %s WHERE device_id = %s"
            destination_cursor.execute(query, (result[0], result[1], result[2], anchor))

    else:
        print("查询结果为空")
destination_conn.commit()
destination_conn.close()
source_conn.close()
